/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class repostData {

    public static Map<String, Integer> latelyrepost = new HashMap<String, Integer>();
    public static int s1 = 1;//我评论给原微博
    public static int s2 = 1;//我回复某人
    public static int s3 = 1;//我回复某人，但是ID取不到，算作评论给原微博
    public static int s4 = 1;//我在评论中@某人
    public static int s5 = 1;//别人评论我
    public static int s6 = 1;//别人在评论中@我
    public static Date afterDate = Calendar.getInstance().getTime();
    public static final Date today = Calendar.getInstance().getTime();

    public static void repostData(DataPreparation data) throws FileNotFoundException, IOException, WeiboException, InterruptedException {
        afterDate.setDate(today.getDate() - Config.iDate);
        repostDataFromMe(data);
        repostToMeData(data);
    }

    public static void repostToMeData(DataPreparation data) throws WeiboException, InterruptedException {
        Timeline cm = new Timeline();
        List<Status> reposts = new ArrayList<Status>();
        int i = 1;
        while (true) {

            List<Status> tmp = null;
            while (tmp == null) {

                tmp = cm.getMentions(new Paging(i++, 50), 0, 0, 0);
                if (tmp != null) {
                    break;
                }
                Thread.sleep(3000);
            }
            if (i > 100) {
                break;
            }
            if (tmp.isEmpty()) {
                break;
            } else {
                reposts.addAll(tmp);
            }
        }

        for (Status c : reposts) {
            if (c.getUser() == null) {
                continue;
            }
            String uid = c.getUser().getId();
            if (c.getCreatedAt().after(afterDate)) {
                if (data.data.latest.containsKey(uid)) {
                    int newc = data.data.latest.get(uid) + 1;
                    data.data.latest.put(uid, newc);
                } else {
                    data.data.latest.put(uid, 1);
                }
            }
            if (!data.data.mFIDs.contains(uid)) {
                continue;
            }
            int newc = 0;
            if (data.data.repost.containsKey(uid)) {
                newc = data.data.repost.get(uid);
            }
            newc += 1;
            data.data.repost.put(uid, newc);
        }


    }

    public static void repostDataFromMe(DataPreparation data) throws WeiboException {
        Timeline cm = new Timeline();

        List<Status> reposts = new ArrayList<Status>();
        int i = 1;
        while (true) {
            List<Status> tmp = cm.getRepostByMe(new Paging(i++, 200));
            if (i > 100) {
                break;
            }
            if (tmp.isEmpty()) {
                break;
            } else {
                reposts.addAll(tmp);
            }
        }
        int j = 1;
        for (Status s : reposts) {
            if (s.getRetweetedStatus() == null || s.getRetweetedStatus().getUser() == null) {
                continue;
            }
            String toStatusUser = s.getRetweetedStatus().getUser().getId();
            List<String> toReplyUser = StringAnalyse(s.getText());
            if (toReplyUser.isEmpty()) {
                int newCount = 0;
                int newlatelyCount = 0;
                if (!data.data.mFIDs.contains(toStatusUser)) {
                    continue;
                }
                if (data.data.repost.containsKey(toStatusUser)) {
                    newCount = data.data.repost.get(toStatusUser);
                }
                if (s.getCreatedAt().after(afterDate)) {
                    if (data.data.latest.containsKey(toStatusUser)) {
                        int newc = data.data.latest.get(toStatusUser) + 1;
                        data.data.latest.put(toStatusUser, newc);
                    } else {
                        data.data.latest.put(toStatusUser, 1);
                    }
                }
                data.data.repost.put(toStatusUser, newCount + 1);

            } else {
                for (String to : toReplyUser) {
                    if (!data.data.mFIDs.contains(to)) {
                        continue;
                    }
                    String toID = data.data.username2id.get(to);
                    if (toID != null) {
                        if (s.getCreatedAt().after(afterDate)) {
                            if (data.data.latest.containsKey(toID)) {
                                int newc = data.data.latest.get(toID) + 1;
                                data.data.latest.put(toID, newc);
                            } else {
                                data.data.latest.put(toID, 1);
                            }
                        }
                        int newCount = 0;
                        int newlatelyCount = 0;
                        if (data.data.repost.containsKey(toID)) {
                            newCount = data.data.repost.get(toID);
                        }
                        if (latelyrepost.containsKey(toStatusUser)) {
                            newlatelyCount = latelyrepost.get(toStatusUser);
                        }
                        data.data.repost.put(toID, newCount + 1);
                    }
                }
            }
        }
    }

    public static List<String> StringAnalyse(String s) {
        List<String> toUser = new ArrayList<String>();
        {
            String b[] = s.split("//@");
            String stringPrePost = b[0];
            int atPosition = stringPrePost.indexOf("@");
            if (atPosition >= 0) {
                String[] strings = b[0].split("@");
                int stringsCount = strings.length;
                for (int i = 0; i < stringsCount; i++) {
                    String user = strings[i].split(" ")[0];
                    if (!user.isEmpty()) {
                        toUser.add(user);
                    }
                }
            }
            if (b.length > 1) {
                int end = b[1].indexOf(":");
                if (end > 0) {
                    toUser.add(b[1].split(":")[0]);
                }
            }
            return toUser;
        }
    }
}
